package io.renren.modules.lucky.controller;


import com.alibaba.fastjson2.JSON;
import io.renren.common.utils.Result;
import io.renren.modules.lucky.dto.background.LuckyCallBackReq;
import io.renren.modules.lucky.dto.background.LuckyClickReq;
import io.renren.modules.lucky.dto.background.LuckyIndexReq;
import io.renren.modules.lucky.dto.LuckyReportDto;
import io.renren.modules.lucky.dto.background.ReportReqDto;
import io.renren.modules.lucky.service.LuckyBaseService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@RequestMapping("api/base")
//@RequestMapping("api/test")
@Api(tags = "招财猫基础接口")
@AllArgsConstructor
public class LuckyBaseController {
    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(LuckyBaseController.class);

    private final LuckyBaseService luckyBaseService;


    @PostMapping("/report")
    @ApiOperation("埋点上报")
    public Result report(@RequestBody LuckyReportDto reportDto) {
        logger.info("上报埋点: {}", JSON.toJSONString(reportDto));
        Result result = luckyBaseService.report(reportDto);
        logger.info("上报埋点返回: {}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/click")
    @ApiOperation("点击上报")
    public Result click(@RequestBody LuckyClickReq clickReq) {
        logger.info("点击上报: {}", JSON.toJSONString(clickReq));
        Result result = luckyBaseService.click(clickReq);
        logger.info("点击上报返回: {}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/success")
    @ApiOperation("订单成功上报")
    public Result order(@RequestBody LuckyCallBackReq callBackReq) {
        logger.info("订单成功上报: {}", JSON.toJSONString(callBackReq));
        Result result = luckyBaseService.order(callBackReq);
        logger.info("订单成功上报返回: {}", JSON.toJSONString(result));
        return result;
    }

    //
    @PostMapping("/reportQuery")
    @ApiOperation("收益报表查询")
    public Result reportQuery(@RequestBody ReportReqDto reportReq) {
        UserDetail user = SecurityUser.getUser();
        logger.info("收益报表查询: {}", JSON.toJSONString(reportReq));
        Result result = luckyBaseService.reportQuery(reportReq, user);
        logger.info("收益报表查询返回: {}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/index")
    @ApiOperation("主页数据")
    public Result index(@RequestBody LuckyIndexReq indexReq) {
        UserDetail user = SecurityUser.getUser();
        logger.info("主页数据查询: {}", JSON.toJSONString(indexReq));
        Result result = luckyBaseService.index(indexReq, user);
        logger.info("主页数据查询返回: {}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/indexCharts")
    @ApiOperation("主页报表数据")
    public Result indexCharts(@RequestBody LuckyIndexReq indexReq) {
        UserDetail user = SecurityUser.getUser();
        logger.info("主页数据报表查询: {}", JSON.toJSONString(indexReq));
        Result result = luckyBaseService.indexCharts(indexReq, user);
        logger.info("主页数据报表查询返回: {}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/uploadFile")
    @ApiOperation("上传发票或打款文件")
    public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) {
        UserDetail user = SecurityUser.getUser();
        /*UserDetail user = new UserDetail();
        user.setId(1876884943376994306L);*/

        logger.info("用户{}上传文件: {}", user.getUsername(), file.getOriginalFilename());
        String fileName = luckyBaseService.uploadFile(file, user);
        logger.info("上传文件返回: {}", fileName);
        return ResponseEntity.ok(fileName);
    }

    @GetMapping("/download")
    public ResponseEntity<byte[]> downloadFile(@RequestParam String fileName) {
        UserDetail user = SecurityUser.getUser();
        /*UserDetail user = new UserDetail();
        user.setId(1876884943376994306L);*/
        try {
            byte[] fileBytes = luckyBaseService.downloadFile(fileName, user);
            HttpHeaders headers = new HttpHeaders();
            // 对文件名进行URL编码
            String encodedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName);

            return new ResponseEntity<>(fileBytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
}
